import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;

public class PLIsTriangle extends PLShape {
	private static final long serialVersionUID = 1L;
	float mWidth;

	public PLIsTriangle() {		
		super(ShapeIdT.kTriangle);
	}

	// if no width, then equliateral triangle is passed 
	public PLIsTriangle(Point start, Point end){
		super(ShapeIdT.kTriangle);
		Parse(start, end, 0);
	}

	public PLIsTriangle(Point start, Point end, float width){
		super(ShapeIdT.kTriangle);
		Parse(start, end, width);
	}
	
	public boolean Parse(Point start, Point end, float width) {
		super.Parse(start, end);
		
		if (width == 0) {
			mWidth = (float)(2*GetHeight()/Math.sqrt(3));
		} else {
			mWidth = 2* width;
		}
		
		return true;
	}
	
	// Attributes 
	public float GetHeight() { return (float)(Math.sqrt((mStart.x - mEnd.x)*(mStart.x - mEnd.x) + (mStart.y - mEnd.y)*(mStart.y - mEnd.y))); }
	public float GetWidth() { return mWidth; }
	public float GetLength() { return (float)(Math.sqrt(GetWidth()*GetWidth()/4 + GetHeight()*GetHeight())); }
	public float GetSlope() { 
			// a negative since left top = 0,0
		if ((mStart.x - mEnd.x)==0){
			return (Float.POSITIVE_INFINITY);
		}
		else{
			return -((float)(mStart.y - mEnd.y))/(mStart.x - mEnd.x);
		}
	} 

	public float GetSlopeAngle() { 
		return (float)Math.atan(GetSlope());
	} 

	public float GetArea() { return (GetHeight() * GetWidth())/2; }	
 
	public Point GetCenter() { 
		Point centroid = new Point();
		centroid.x = mStart.x;		
		centroid.y = (int)(mStart.y  - GetHeight()*2/3);
		return centroid;
	}
		
	public Shape GetJavaShape(){
		Polygon p = new Polygon(); 
		Point[] corners = GetCorners();
		p.addPoint(corners[0].x, corners[0].y); 
		p.addPoint(corners[1].x, corners[1].y); 
		p.addPoint(corners[2].x, corners[2].y); 

		return p; 
	}
	
	// Draw
	public void Draw(Graphics g) {
	        g.drawPolygon((Polygon) GetJavaShape()); 
	}		

	// PROTECTED 
	
	protected void SetupLines() {	
		mLines = new PLLine[3]; 

		Point[] corners = GetCorners();
		
		mLines[0] = new PLLine(corners[0], corners[1]);
		mLines[1] = new PLLine(corners[1], corners[2]);
		mLines[2] = new PLLine(corners[2], corners[0]);
	}
	
	protected void SetupCorners() {
		mCorners = new Point[3]; 
		
		mCorners[0] = new Point (mStart.x, mStart.y); 

		PLLine line = new PLLine(mStart, mEnd);
		float slopeAngle = line.GetSlopeAngle() + (float)Math.PI;
		Point[] points = PLShapeUtils.GetPossibleEndPoints(mEnd, slopeAngle, GetWidth()/2);
		
			
		mCorners[1] = points[0]; 
		mCorners[2] = points[1]; 
	}		
}

